package com.test.applet;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;

public class MyClient extends Applet implements ActionListener {
	private static final long serialVersionUID = 1L;
	Socket localSocket;
	PrintWriter out;
	BufferedReader in;
	String s;
	private Button b;
	private TextField t1, t2;

	public void init() {
		setLayout(new FlowLayout());
		t1 = new TextField(20);
		t1.setText("Please Enter the value here");
		add(t1);
		t2 = new TextField(20);
		add(t2);
		b = new Button("Connect");
		b.addActionListener(this);
		add(b);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b)
			// Create a socket
			try {
				localSocket = new Socket(this.getCodeBase().getHost(), 2000);
				// Setup data stream in and out of socket and from KeyBoard
				in = new BufferedReader(new InputStreamReader(localSocket.getInputStream()));
				out = new PrintWriter(localSocket.getOutputStream());
				// While we have a connection
				// get textfield value
				// String s = t1.getText();
				// Read Texfield value
				out.println(t1.getText());
				// flush the buffer if not full!
				out.flush();
				// read incoming string from socket
				// System.out.println(in.readLine());
				String line = in.readLine();
				t2.setText(line);
			} catch (UnknownHostException unc) {
				System.out.println("Connection why not connected");
			} catch (IOException ioe) {
				System.out.println(ioe.getMessage());
			} 
//			finally {
//				System.out.println("Close the socket connection.");
//				try {
//					localSocket.close();
//				} catch (IOException e1) {
//					e1.printStackTrace();
//				}
//			}
	}

	public static void main(String[] args) {
		MyClient applet = new MyClient();
		JFrame frame = new JFrame();
		frame.setTitle("Socket Test");
		frame.getContentPane().add(applet, BorderLayout.CENTER);
		applet.init();
		applet.start();
		frame.setSize(460, 360);
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
		frame.setVisible(true);
	}
}